package com.example.mymnue;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class DishActivity extends AppCompatActivity implements Runnable, AdapterView.OnItemClickListener {
    Handler handler;
    private static final String TAG = "AA";
    private List<HashMap<String, Object>> listItems; // 存放文字、图片信息
    private SimpleAdapter listItemAdapter; // 适配器
    private int msgWhat = 7;
    HashMap<String, Object> map;
    Document doc;
    String imgStr;
    Intent intent;
    String DZ;

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dish);
        ListView mlist = findViewById(R.id.mlist);
        intent = getIntent();
        DZ = intent.getStringExtra("dish");
        initListView();
        mlist.setAdapter(listItemAdapter);
        Thread t = new Thread(this); // 创建新线程
        t.start(); // 开启线程
        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == msgWhat) {
                    listItems = (List<HashMap<String, Object>>) msg.obj;
                    Log.i(TAG, "handleMessage: " + listItems);
                    mlist.setAdapter(new SimpleAdapter(DishActivity.this, listItems,
                            R.layout.list_item,
                            new String[]{"TV1", "TV2"},
                            new int[]{R.id.textView3, R.id.textView4}
                    ));
                }
                super.handleMessage(msg);
            }
        };
        mlist.setOnItemClickListener(this);
    }

    private void initListView() {
        listItems = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            map = new HashMap<String, Object>();
            map.put("TV1", "名称" + i); // 详情描述
            map.put("TV2", "原料：" + i);
            listItems.add(map);
        }
        // 生成适配器的Item和动态数组对应的元素
        listItemAdapter = new SimpleAdapter(this, listItems, // listItems数据源
                R.layout.list_item, // ListItem的XML布局实现
                new String[]{"TV1", "TV2"},
                new int[]{R.id.textView9, R.id.textView11}
        );
    }

    @Override//获取数据
    public void run() {
        Log.i(TAG, "run: run()......");
        for (int i = 1; i < 3; i++) {
            Log.i(TAG, "run: i=" + i);
        }
        listItems = new ArrayList<>();
        try {
            doc = Jsoup.connect(DZ).get();
            Elements tds = doc.select("li");
            Log.i(TAG, "run: " + tds.text());
            for (int i = 23; i < tds.size(); i++) {
                Element td = tds.get(i).select("a").get(1);
                String href = td.attr("href");
                Log.i(TAG, "run: href" + href);
                Element img = tds.get(i).select("img").get(0);
                imgStr = img.attr("data-src");
                Log.i(TAG, "run: imgStr" + imgStr);
                Element td2 = tds.get(i).select("p").last();
                Log.i(TAG, "run1: " + td.text());
                Log.i(TAG, "run2: " + td2.text());
                String tdStr = td.text();
                String pStr = td2.text();
                map = new HashMap<String, Object>();
                map.put("TV1", tdStr);
                map.put("TV2", pStr);
                map.put("DZ", href);
                listItems.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage();
        msg.what = msgWhat;
        msg.obj = listItems;
        handler.sendMessage(msg);

        Log.i("thread", "sendMessage.....");

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> map1 = listItems.get(position);
        Log.i(TAG, "onItemClick: map1="+map1);
        String detailStr = (String) map1.get("TV2");
        String title = (String) map1.get("TV1");
        String dz =(String) map1.get("DZ");
        Log.i(TAG, "onItemClick:tv2 "+detailStr);
        Log.i(TAG, "onItemClick:tv1 "+title);
        Log.i(TAG, "onItemClick: DZ "+dz );
        Intent rateCalc = new Intent(this,DetailActivity.class);
        rateCalc.putExtra("title",title);
        rateCalc.putExtra("yuanliao",detailStr);
        rateCalc.putExtra("DZ",dz);
        startActivityForResult(rateCalc,1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.shop:
                Intent intent1=new Intent(DishActivity.this,ShopActivity.class);
                startActivity(intent1);
                break;
            case R.id.history:
                Intent intent2 =new Intent(DishActivity.this,HistoryActivity.class);
                startActivity(intent2);
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}